/**
 * 
 */
package com.platform.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * <pre>
 * 功能：数字的格式化
 * </pre>
 * 
 * @author wei.gao
 */
public class FormatUtils {
	private static final NumberFormat format_number = new DecimalFormat("0.00");
	private static final DecimalFormat format_decimal = new DecimalFormat("#.##");
	private static final String format_mobile = "(\\d{3})\\d{4}(\\d{4})";
	private static final String format_credential = "(\\d{6})\\d{8}(\\w{4})";
	private static final String format_card = "(\\d{4})\\d{4,}(\\d{4})";
	
	public static final String XING_STRING = "*";
	public static final String SECURITY_MASK_MOBILE2 = "**";
	public static final String SECURITY_MASK_MOBILE3 = "***";
	public static final String SECURITY_MASK_MOBILE = "****";
	
	/**
	 * <pre>
	 * 功能：保留两位小数
	 * </pre>
	 * 
	 * @param money
	 * @return
	 */
	public static BigDecimal format(BigDecimal money) {
		return format(money, 2);
	}
	
	/**
	 * <pre>
	 * 功能：保留n位小数
	 * </pre>
	 * 
	 * @param money
	 * @param n
	 * @return
	 */
	public static BigDecimal format(BigDecimal money, int n) {
		return money.divide(new BigDecimal("1"), n, BigDecimal.ROUND_HALF_DOWN);
	}
	
	/**
	 * <pre>
	 * 功能：格式化数字，保留两位
	 * </pre>
	 * 
	 * @param decimal
	 * @return
	 */
	public static String formatNumber(BigDecimal decimal) {
		return formatNumber(decimal, format_number);
	}
	
	/**
	 * <pre>
	 * 功能：格式化数字
	 * </pre>
	 * 
	 * @param decimal
	 * @param format
	 * @return
	 */
	public static String formatNumber(BigDecimal decimal, NumberFormat format) {
		if (decimal == null) {
			return format.format(0);
		}
		return format.format(decimal.doubleValue());
	}
	
	/**
	 * <pre>
	 * 功能：格式化数字，小于10000的为金额，大于10000的为3.13万
	 * </pre>
	 * 
	 * @param decimal
	 * @return
	 */
	public static String formatWanYuan(BigDecimal decimal) {
		if (decimal == null) {
			return "";
		}
		if (decimal.intValue() < 0) {
			return "";
		} else if (decimal.intValue() < 10000) {
			return decimal.intValue() + "";
		} else {
			return formatNumber(new BigDecimal(decimal.divide(new BigDecimal(10000)).doubleValue())) + "万";
		}
	}
	
	/**
	 * <pre>
	 * 功能：格式化手机号码
	 * </pre>
	 * 
	 * @param mobile
	 * @return
	 */
	public static String formatMobile(String mobile) {
		if (mobile == null) {
			return "";
		}
		return mobile.replaceAll(format_mobile, "$1****$2");
	}
	
	/**
	 * <pre>
	 * 功能：格式化身份证
	 * </pre>
	 * 
	 * @param credentials
	 * @return
	 */
	public static String formatCredentials(String credentials) {
		if (credentials == null) {
			return "";
		}
		return credentials.replaceAll(format_credential, "$1*****$2");
	}
	
	/**
	 * <pre>
	 * 功能：格式化银行卡
	 * </pre>
	 * 
	 * @param card
	 * @return
	 */
	public static String formatCard(String card) {
		if (card == null) {
			return "";
		}
		return card.replaceAll(format_card, "$1********$2");
	}
	
	/**
	 * <pre>
	 * 功能：格式化银行卡后四位
	 * </pre>
	 * 
	 * @param card
	 * @return
	 */
	public static String formatCardLastFour(String card) {
		if (card == null) {
			return "";
		}
		return card.replaceAll(format_card, "$2");
	}
	
	/**
	 * <pre>
	 * 功能：格式化金额按三位逗号分割
	 * </pre>
	 * 
	 * @param money
	 * @return
	 */
	public static String formatDecimal(BigDecimal money) {
		if (money.equals(BigDecimal.ZERO)) {
			return "0.00";
		}
		return format_decimal.format(money);
	}
	
	/**
	 * <pre>
	 * 功能：排行榜姓名脱敏，只留姓名的最后一个字
	 * </pre>
	 * 
	 * @param name
	 * @return
	 */
	public static String formatUsername(String name) {
		if (StringUtils.isNotBlank(name)) {
			StringBuilder sb = new StringBuilder();
			if (name.length() == 2) {
				sb.append(XING_STRING);
				sb.append(name.substring(1, name.length()));
			} else if (name.length() == 3) {
				sb.append(SECURITY_MASK_MOBILE2);
				sb.append(name.substring(2, name.length()));
			} else if (name.length() == 4) {
				sb.append(SECURITY_MASK_MOBILE2);
				sb.append(name.substring(3, name.length()));
			} else {
				sb.append(SECURITY_MASK_MOBILE2);
				sb.append(name.substring(name.length() - 1, name.length()));
			}
			return sb.toString();
		}
		return name;
	}
}
